<?php
	class LinkedList{
		private $firstNode;
		private $lastNode;
		private $count;

		public function __construct(){
			$this->firstNode=NULL;
			$this->lastNode=NULL;
			$this->count=0;
		}
		
		public function __destruct(){
			unset($this->firstNode);
			unset($this->lastNode);
			unset($this->count);
		}
		
		public function __autoload(){
			require_once("ListNode.php");
		}
      
	     public function setFirstNode($firstNode){
	       $this->firstNode=$firstNode;
	     }
	     
	     public function getFirstNode(){
	       return $this->firstNode;
	     }
	     
	     public function setLastNode($lastNode){
	     	$this->lastNode=$lastNode;
	     }
	     
	     public function getLastNode(){
	     	return $this->lastNode;
	     }
	     
	     public function setCount($count){
	     	$this->count=$count;
	     }
	     
	     public function getCount(){
	     	return $this->count;
	     }
	     
	     public function addFirst($node){
	     	if($this->count==0){
	     		$this->firstNode=$node;
	     		$this->lastNode=$node;
	     	}
	     	else{
	     		$node->setNext($this->firstNode);
	     		$this->firstNode->setPrevious($node);	
	     		$this->firstNode=$node;
	     	}
	     	$this->count++;
	     }
	     
	     public function addLast($node){
	     	if($this->count==0){
	     		$this->firstNode=$node;
	     		$this->lastNode=$node;
	     	}
	     	else{
	     		$node->setPrevious($this->lastNode);
	     		$this->lastNode->setNext($node);
	     		$this->lastNode=$node;
	     	}
	     	$this->count++;
	     }
	     
	     public function deleteFirst(){
	     	if($this->count!=0){
	     		$this->firstNode=$this->firstNode->getNext();
	     		$this->firstNode->setPrevious(NULL);
	     		$this->count--;
	     	}
	     }
	     
	     public function deleteLast(){
	     	if($this->count!=0){
	     		$this->lastNode=$this->lastNode->getPrevious();
	     		$this->lastNode->setNext(NULL);
	     		$this->count--;
	     	}
	     }
		
	     public function display(){
	     	$current=$this->firstNode;
	     	while($current!=$this->lastNode){
	     		$current->display();
	     		$current=$current->getNext();
	     	}
	     	$this->lastNode->display();
	     }
	}
?>